package com.bauer.base.datastructure.stack.express;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author ：rui.wei
 * @Date ：Created in 16:34 2019/7/17
 * @Description：
 */
public class TestMain {

    private static List<Character> symbolStack = new ArrayList<>();

    private static List<Bool> data = new ArrayList<>();

    private static Set<Character> symbol = new HashSet<>();


    static {
        symbol.add('|');
        symbol.add('&');
    }


    public static void main(String[] args) {
        String expression = "E|((A&B|C)&D)";
        Map<String, Condition> conditionMap = new HashMap<>(16);
        conditionMap.put("A", new Condition("A"));
        conditionMap.put("B", new Condition("B"));
        conditionMap.put("C", new Condition("C"));
        conditionMap.put("D", new Condition("D"));
        conditionMap.put("E", new Condition("E"));
        for (Character c : expression.toCharArray()) {

            //字母
            if ('A' <= c && 'Z' >= c) {
                //符号栈顶为运算符   且 数据栈顶为数据  运算
                if (getLast(data) != null && symbol.contains(getLast(symbolStack))) {
                    Bool bool = removeLast(data);
                    Character currentSymbol = removeLast(symbolStack);
                    Bool bool1 = new Bool();
                    Condition currentCondition = conditionMap.get(c.toString());

                    if (currentSymbol == '|' && bool.getCache() != null) {
                        bool1.setShould(new Object[]{bool.getCache(), currentCondition});
                        data.add(bool1);
                        continue;
                    }

                    if (currentSymbol == '|') {
                        bool1.setShould(new Object[]{bool, currentCondition});
                        data.add(bool1);
                        continue;
                    }

                    if (currentSymbol == '&' && bool.getCache() != null) {
                        bool1.setMust(new Object[]{bool.getCache(), currentCondition});
                        data.add(bool1);
                        continue;
                    }

                    if (currentSymbol == '&') {
                        bool1.setMust(new Object[]{bool, currentCondition});
                        data.add(bool1);
                        continue;
                    }
                    continue;

                }
                data.add(new Bool(conditionMap.get(c.toString())));
            } else if (c == '|' || c == '&' || c == '(') {
                symbolStack.add(c);
            } else {
                Character removeLast = removeLast(symbolStack);
                if (removeLast != '(') {
                    System.out.println("解析失败");
                }
            }
        }

        //最后一次计算
        Character character = removeLast(symbolStack);
        if (character != null) {
            Bool bool1 = new Bool();
            if (character == '&') {
                bool1.setMust(new Object[]{removeLast(data), removeLast(data)});
            } else {
                bool1.setShould(new Object[]{removeLast(data), removeLast(data)});
            }
            data.add(bool1);
        }

        System.out.println(JSON.toJSONString(removeLast(data)));

    }


    private static <T> T getLast(List<T> t) {
        if (t.size() == 0) {
            return null;
        }
        return t.get(t.size() - 1);
    }


    private static <T> T removeLast(List<T> t) {
        if (t.size() == 0) {
            return null;
        }
        return t.remove(t.size() - 1);
    }

}
